#include "events_manager.h"

using namespace alice;

EventsManager* EventsManager::instance_ = nullptr;

void EventsManager::Listen() {
  SDL_PollEvent(&event_);
  switch (event_.type) {
    case SDL_QUIT:
      Director::GetInstance()->Quit();
      break;
    case SDL_KEYDOWN:
      KeyDown();
      break;
    case SDL_KEYUP:
      KeyUp();
      break;
    default:
      break;
  }
}

bool EventsManager::IsKeyDown(SDL_Scancode key) {
  if (key_states_[key] == 1) {
    return true;
  }
  return false;
}

void EventsManager::KeyDown() {
  key_states_ = SDL_GetKeyboardState(nullptr);
}

void EventsManager::KeyUp() {
  key_states_ = SDL_GetKeyboardState(nullptr);
}

int EventsManager::GetAxisKey(Axis axis) {
  int key = 0;
  switch (axis) {
    case HORIZONTAL:
      if (IsKeyDown(SDL_SCANCODE_D)) {
        key = 1;
      } else if (IsKeyDown(SDL_SCANCODE_A)) {
        key = -1;
      }
      break;

    case VERTICAL:
      if (IsKeyDown(SDL_SCANCODE_W)) {
        key = 1;
      } else if (IsKeyDown(SDL_SCANCODE_S)) {
        key = -1;
      }
      break;

    default:
      key = 0;
      break;
  }
  return key;
}